package net.realsee.controller.wcs.work;

import cn.dev33.satoken.annotation.SaCheckPermission;
import lombok.RequiredArgsConstructor;
import net.realsee.common.annotation.Log;
import net.realsee.common.annotation.RepeatSubmit;
import net.realsee.common.enums.BusinessType;
import net.realsee.common.utils.poi.ExcelUtil;
import net.realsee.domain.wcs.work.bo.WcsOrderTypeBo;
import net.realsee.domain.wcs.work.vo.WcsOrderTypeVo;
import net.realsee.framework.core.controller.BaseController;
import net.realsee.framework.core.domain.PageQuery;
import net.realsee.framework.core.domain.R;
import net.realsee.framework.core.page.TableDataInfo;
import net.realsee.framework.core.validate.AddGroup;
import net.realsee.framework.core.validate.EditGroup;
import net.realsee.service.wcs.work.IWcsOrderTypeService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 单据类型
 *
 * @author realsee
 * @date 2023-08-11
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/wcs/orderType")
public class WcsOrderTypeController extends BaseController {

    private final IWcsOrderTypeService iWcsOrderTypeService;

    /**
     * 查询单据类型列表
     */
    @GetMapping("/list")
    public TableDataInfo<WcsOrderTypeVo> list(WcsOrderTypeBo bo, PageQuery pageQuery) {
        return iWcsOrderTypeService.selectVoPage(bo, pageQuery);
    }

    /**
     * 导出单据类型列表
     */
    @SaCheckPermission("wcs:orderType:export")
    @Log(title = "单据类型", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(WcsOrderTypeBo bo, HttpServletResponse response) {
        List<WcsOrderTypeVo> list = iWcsOrderTypeService.selectVoList(bo);
        ExcelUtil.exportExcel(list, "单据类型", WcsOrderTypeVo.class, response);
    }

    /**
     * 获取单据类型详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("wcs:orderType:query")
    @GetMapping("/{id}")
    public R<WcsOrderTypeVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(iWcsOrderTypeService.selectVoById(id));
    }

    /**
     * 新增单据类型
     */
    @SaCheckPermission("wcs:orderType:add")
    @Log(title = "单据类型", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody WcsOrderTypeBo bo) {
        return toAjax(iWcsOrderTypeService.insertByBo(bo));
    }

    /**
     * 修改单据类型
     */
    @SaCheckPermission("wcs:orderType:edit")
    @Log(title = "单据类型", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody WcsOrderTypeBo bo) {
        return toAjax(iWcsOrderTypeService.updateByBo(bo));
    }

    /**
     * 删除单据类型
     *
     * @param ids 主键串
     */
    @SaCheckPermission("wcs:orderType:remove")
    @Log(title = "单据类型", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空") @PathVariable Long[] ids) {
        return toAjax(iWcsOrderTypeService.deleteWithValidByIds(Arrays.asList(ids)));
    }
}
